package com.usts2022.classcostrecord;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.usts2022.classcostrecord.Utils.HttpUtils;
import com.usts2022.classcostrecord.entity.User;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class LoginFragment extends Fragment {

    public static final int LOGIN_RESULT_CODE = 222;
    private Button mLogin;
    private EditText mAccount, mPassword;
    private static final String TAG = "loginActivity";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        //绑定
        mLogin = view.findViewById(R.id.login_btn_login);
        mAccount = view.findViewById(R.id.login_account);
        mPassword = view.findViewById(R.id.login_password);

        //登录事件
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String url = "/user/login/" + mAccount.getText().toString() + "/" + mPassword.getText().toString();
                try {
                    HttpUtils.get(url, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Toast.makeText(getActivity(), "请求失败,密码或账号错误", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            if (Looper.myLooper() == null)
                                Looper.prepare();
                            String str = response.body().string();
                            if (response.code() != 200 || str.equals("")) {
                                Toast.makeText(getActivity(), "登录失败,密码或账号错误", Toast.LENGTH_SHORT).show();
                            } else {
                                User user = JSON.parseObject(str, User.class);
                                //保存登录状态
                                saveData(mAccount.getText().toString(), mPassword.getText().toString());
                                Toast.makeText(getActivity(), "登录成功,欢迎你!" + user.getName(), Toast.LENGTH_SHORT).show();
                                //传递信息
                                getActivity().setResult(LOGIN_RESULT_CODE);
                                getActivity().finish();
                            }
                            Looper.loop();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        return view;
    }


    public void saveData(String account, String password) {
        FileOutputStream out = null;
        BufferedWriter writer = null;
        String data = account + " " + password;
        try {
            //userName  表示文件的名称
            //MODE_APPEND 表示从文件后面接着写入，不会覆盖之前的数据
            //MODE_PRIVATE 表示会重新覆盖之前写入的数据
            out = getActivity().openFileOutput("loginData", Context.MODE_PRIVATE);
            writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}